
/*
Copyright 2009 Peter Ekler, Akos Ludanyi, Budapest University of Technology and Economics, Department of Automation and Applied Informatics, Applied Mobile Research Group (AMORG) http://amorg.aut.bme.hu

This file is part of MobTorrent.

MobTorrent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MobTorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MobTorrent.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * MTNetworkManager.java
 *
 * Created on July 20, 2007, 2:18 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package TorrentEngine;

import Logger.MTLogger;
import Settings.Preferences;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;

/**
 *
 * @author ekler
 */
public class MTNetworkManager {
    
    private MTTorrentManager torrentManager;
    private boolean isListening = false;
    private ServerSocketConnection serverconn;
    private AcceptConnectionThread acceptConnectionThread;
    
    /** Creates a new instance of MTNetworkManager */
    public MTNetworkManager(MTTorrentManager aTorrentManager)
    {
        // get the torrent manager
        torrentManager = aTorrentManager;
    }
    
    // accept connections
    public class AcceptConnectionThread extends Thread
    {
        public void run()
        {
            while(isListening)
            {
                try {
                    MTLogger.writeLine("--- Wait for accepting");
                    // acceppt the connection
                    SocketConnection sc = (SocketConnection)serverconn.acceptAndOpen();
                    // register the incomming connection
                    torrentManager.registerIncomingConnection(sc);
                    MTLogger.writeLine("--- CONNECTION ACCEPTED!!!");
                    sleep(3000);
                } catch (IOException ex) {
                    ex.printStackTrace();
                    MTLogger.writeLine("--- ACCEPT TERMINTATED: "+ex.getMessage());
                } catch (Exception ex) {
                    ex.printStackTrace();
                    MTLogger.writeLine("--- ACCEPT TERMINTATED(e): "+ex.getMessage());
                }
            }
        }
    }
    
    // start listening for incomming connections
    public void startListening()
    {
        if (!isListening)
        {
            try {
                MTLogger.writeLine("--- START LISTENING");
                serverconn = (ServerSocketConnection)MTNetworkStatusManager.connect("socket://:" + Preferences.IncommingPort, Connector.READ_WRITE);
                Preferences.LocalAddress = serverconn.getLocalAddress();
                MTLogger.writeLine("--- LISTENING STARTED ON: "+serverconn.getLocalAddress()+":"+serverconn.getLocalPort());
                isListening = true;
                acceptConnectionThread = new AcceptConnectionThread();
                acceptConnectionThread.start();          
            } catch (Exception ex) {
                MTLogger.writeLine("Server socket open error: "+ex.getMessage());
            }
        }
    } 

    // stop listening
    public void stopListening() {
        isListening = false;
        // close connection
        try {
            if (serverconn!=null)
                serverconn.close();
        } catch (IOException ex) {
            MTLogger.writeLine("Server socket close error: "+ex.getMessage());
        }
        // terminate acceptThread
        try {
            acceptConnectionThread = null;
            //if (acceptConnectionThread!=null)
            //    acceptConnectionThread.join();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

